/*
 * Copyright (c) 2009 Project E7
 *
 * This file is part of ZebBowling
 *
 * ZebBowling is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ZebBowling is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ZebBowling. If not, see <http://www.gnu.org/licenses/>.
 **/

package org.zebbowling.world;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Timer;

/**
 *
 * @author markvismonte
 */
public class BowlingMenu extends Canvas implements KeyListener, ActionListener
    {
    private final String[] GAME_MODE = {"2 Player Mode", "3 Player Mode",
        "4 Player Mode"};
    private int mode;//must be an index of GAME_MODE
    private boolean flash;
    private Timer flasher;
    public BowlingFrame frame;

    /**
     * Creates a new Bowling Menu
     */
    public BowlingMenu()
        {
        super();
        setBackground(BowlingLane.ALLEY_COLOR);
        mode = 0;
        flasher = new Timer(300, this);
        repaint();
        flasher.start();
        flash = false;
        }

    public void paint(Graphics g)
        {
        //Set up double buffering
        BufferedImage back = (BufferedImage) (createImage(getWidth(), getHeight()));
        Graphics graphToBack = back.createGraphics();



        //Draws buffered image to screen
        g.drawImage(back, 0, 0, null);
        }

    /**
     * Adds Frame
     * @param frame - BowlingFrame to be added
     */
    public void setFrame(BowlingFrame frame)
        {
        this.frame = frame;
        }

    /**
     * Removes menu from frame ContentPane and calls startAlley()
     * in BowlingLane class
     */
    private void goToGame()
        {
        frame.getContentPane().remove(this);
        frame.setSize(BowlingFrame.GAME_WIDTH, BowlingFrame.GAME_HEIGHT);
        }

    public void keyTyped(KeyEvent e)
        {
        }

    /**
     * Moves up and down according to arrow keys or spot
     * if enter is pressed, it enter the game with given number of players
     * @param e - keyevent that was pressed
     */
    public void keyPressed(KeyEvent e) 
        {
        mode = e.getKeyCode() == 38? (mode - 1 < 0? mode: mode - 1):mode;
        mode = e.getKeyCode() == 40? (mode + 1 > 2? mode: mode + 1):mode;
        }

    public void keyReleased(KeyEvent e) {
    }

    /**
     * Refreshes Menu
     * @param e
     */
    public void actionPerformed(ActionEvent e) {
        repaint();
    }

}
